package com.example.test.bottom_tab;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.example.test.R;
import com.example.test.login.admin;

public class tab_main_activity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {

    private RadioGroup rg_tab_bar;
    private RadioButton rb_channel;
    public Button bt1,bt2,bt3,bt4,back;

    //Fragment Object
    private main_Fragment a1;
    private tiku_Fragment b1;
    private test_Fragment c1;
    private FragmentManager fManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tab_main_activity);
        fManager = getFragmentManager();
//        bt1 = (Button) findViewById(R.id.bt1);
        rg_tab_bar = (RadioGroup) findViewById(R.id.rg_tab_bar);
        rg_tab_bar.setOnCheckedChangeListener(this);
        //获取第一个单选按钮，并设置其为选中状态
        rb_channel = (RadioButton) findViewById(R.id.rb_channel);
        rb_channel.setChecked(true);
    }


    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        FragmentTransaction fTransaction = fManager.beginTransaction();
        hideAllFragment(fTransaction);
        switch (checkedId){
            case R.id.rb_channel:
                if(a1==null){
                    a1 = new main_Fragment("欢迎！！！",back);
                    fTransaction.add(R.id.ly_content,a1);
                }else {
                    fTransaction.show(a1);
                }
                break;
            case R.id.rb_message:
                if(b1==null){
                    b1 = new tiku_Fragment(bt1,bt2);
                    fTransaction.add(R.id.ly_content,b1);
                }else{
                    fTransaction.show(b1);
                }
                break;
            case R.id.rb_setting:
                if(c1 == null){
                    c1 = new test_Fragment(bt3,bt4);
                    fTransaction.add(R.id.ly_content,c1);
                }else{
                    fTransaction.show(c1);
                }
                break;
        }

        fTransaction.commit();
    }

    //隐藏所有Fragment
    private void hideAllFragment(FragmentTransaction fragmentTransaction){
//        if(fg1 != null)fragmentTransaction.hide(fg1);
        if(a1 != null) fragmentTransaction.hide(a1);
        if(b1 != null) fragmentTransaction.hide(b1);
        if(c1 != null) fragmentTransaction.hide(c1);
//        if(fg2 != null)fragmentTransaction.hide(fg2);
//        if(fg4 != null)fragmentTransaction.hide(fg4);
    }


}